home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8234 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointer arithmetic
  5. Date: 29 Feb 1996 16:37:47 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb29093747@qcd.lanl.gov>
  8. References: <4h2r55$er@s3.iway.fr>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: Pascal Terracol's message of 29 Feb 1996 00:14:28 GMT
  13.  
  14. In article <4h2r55$er@s3.iway.fr>
  15. Pascal Terracol <assetsto@pratique.fr> writes:
  16.  
  17. PT:  Hello,
  18. PT: 
  19. PT: this sample code have been correctly working on a pc 80286 processor
  20. PT: 
  21. PT: I put it on a mac and the pointers seems to act differently...
  22. PT: any idea about that ? 
  23.  
  24. Usually means the code was not C to start with: it was using any
  25. sequence of symbols which was not detected as an error by the compiler
  26. in question.
  27.  
  28. Sometimes, it is valid, but implementation dependent C.
  29.  
  30. PT: 
  31. PT: 
  32. PT:      int size, n1, n2 ;
  33. PT:      Point  *p_debut, *p1, *p2 ;
  34. /* "vecteur" de translation des adr  */ 
  35. PT: 
  36. PT: ...
  37. PT: 
  38. PT:       n1 = (int) (l->p1)/sizeof(Point) ;
  39. PT:          n2 = (int) (l->p2)/sizeof(Point) ;
  40.  
  41. What is l? If you are having a problem, why can't you ask the question
  42. with a short compilable piece of code? Why do you expect us to spend
  43. our time extracting meaning from a garbled piece of non-code, so that
  44. we can help you?
  45.  
  46. In any case, assuming (l->p1) is a Point*, there is no guarantee what
  47. (int)(l->p1)/sizeof(Point) gives. Do not use such a construct: in fact
  48. trying to convert a pointer to int may even crash some machine. If you 
  49. know that l->p1 points into some array and l->p0 is the beginning of
  50. that array, write n1 = (l->p1 - l->p0). Instead why not store the
  51. array index in l?
  52.  
  53.  
  54. PT:         
  55. PT:             p1 = p_debut + n1 ;
  56. PT:             p2 = p_debut + n1 ;
  57. PT: ...
  58. PT: 
  59. PT: is there a cast solution ?
  60.  
  61. I have absolutely no idea what you mean by this question. Try to avoid
  62. casts when you write code: most of what a beginner does with casts is
  63. based on misunderstanding.
  64.  
  65. Cheers
  66. Tanmoy
  67. --
  68. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  69. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  70. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  71. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  72. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  73. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  74.